#include <QStringLiteral> // for qMakeStringPrivate, QStringLiteral
#include <QtGlobal> // for qPrintable, Q_UNUSED
-#include "defs.h" // for Waypoint, fatal, route_head, le_read32, waypt_add, track_add_wpt, track_add_head, xfree, xmalloc, doing_rtes, doing_wpts, gb_color, route_add_head, route_add_wpt, unknown_alt, doing_trks
+#include "defs.h" // for Waypoint, fatal, route_head, le_read32, waypt_add, track_add_wpt, track_add_head, doing_rtes, doing_wpts, gb_color, route_add_head, route_add_wpt, unknown_alt, doing_trks
#include "gbfile.h" // for gbfread, gbfgetc, gbfgetint32, gbfreadbuf, gbfseek, gbfgetdbl, gbfgetint16, gbfclose, gbfgetnativecstr, gbfgetuint16, gbfopen_le
#include "jeeps/gpsmath.h" // for GPS_Math_Known_Datum_To_WGS84_M
*/
// Read the track bytes into a buffer
- auto* buf = (unsigned char*) xmalloc(track_byte_count);
- gbfread(buf, 1, track_byte_count, tpo_file_in);
+ QScopedArrayPointer<unsigned char> buf(new unsigned char[track_byte_count]);
+ gbfread(buf.get(), 1, track_byte_count, tpo_file_in);
// these can be set repeatedly, and retain their value between settings
// (even if not used for every trackpoint)
// Time to read a new latlong?
if (!llvalid) {
- lon = le_read32(buf+jj);
+ lon = le_read32(&buf[jj]);
if constexpr(debug > 3) {
printf("%02x %02x %02x %02x - raw lon = %d (byte %u)\n", buf[jj], buf[jj+1], buf[jj+2], buf[jj+3], lon,jj);
}
jj+=4;
- lat = le_read32(buf+jj);
+ lat = le_read32(&buf[jj]);
if constexpr(debug > 3) {
printf("%02x %02x %02x %02x - raw lat = %d (byte %u)\n", buf[jj], buf[jj+1], buf[jj+2], buf[jj+3], lat,jj);
}
&& !buf[jj+3]
&& !buf[jj+2]) {
- lonscale = le_read32(buf+jj);
+ lonscale = le_read32(&buf[jj]);
if constexpr(debug > 3) {
printf("%02x %02x %02x %02x - raw lon scale = %d (byte %u)\n", buf[jj], buf[jj+1], buf[jj+2], buf[jj+3], lonscale, jj);
}
&& !buf[jj+3]
&& !buf[jj+2]) {
- latscale = le_read32(buf+jj);
+ latscale = le_read32(&buf[jj]);
if constexpr(debug > 3) {
printf("%02x %02x %02x %02x - raw lat scale = %d (byte %u)\n", buf[jj], buf[jj+1], buf[jj+2], buf[jj+3], latscale, jj);
}
// read 8-byte lon+lat, required at start of track or after 0x88 tag
if (tpmode == GetFullPoint) {
- lon = le_read32(buf+jj);
+ lon = le_read32(&buf[jj]);
if constexpr(debug > 3) {
printf("%02x %02x %02x %02x - raw lon = %d (byte %u)\n", buf[jj], buf[jj+1], buf[jj+2], buf[jj+3], lon,jj);
}
jj+=4;
- lat = le_read32(buf+jj);
+ lat = le_read32(&buf[jj]);
if constexpr(debug > 3) {
printf("%02x %02x %02x %02x - raw lat = %d (byte %u)\n", buf[jj], buf[jj+1], buf[jj+2], buf[jj+3], lat,jj);
}
// Note that lonscale can begin with 0x88, which should not be confused with GetFullPoint tags.
if (tpmode == CheckLonScale) {
if ((jj+3<track_byte_count) && !(buf[jj+3]) && !(buf[jj+2])) {
- lonscale = le_read32(buf+jj);
+ lonscale = le_read32(&buf[jj]);
if constexpr(debug > 3) {
printf("%02x %02x %02x %02x - raw lon scale = %d (byte %u)\n", buf[jj], buf[jj+1], buf[jj+2], buf[jj+3], lonscale, jj);
}
// Note that latscale can begin with 0x88, which should not be confused with GetFullPoint tags.
if (tpmode == CheckLatScale) {
if ((jj+3<track_byte_count) && !(buf[jj+3]) && !(buf[jj+2])) {
- latscale = le_read32(buf+jj);
+ latscale = le_read32(&buf[jj]);
if constexpr(debug > 3) {
printf("%02x %02x %02x %02x - raw lat scale = %d (byte %u)\n", buf[jj], buf[jj+1], buf[jj+2], buf[jj+3], latscale, jj);
}
#endif
} // end for jj track_byte_count
- xfree(buf);
} // end for ii track_count
} // end of tpo_process_tracks
// Waypoint decoder for version 3.x files.
//
-void TpoFormatBase::tpo_process_waypoints()
+void TpoFormatBase::tpo_process_waypoints(QList<Waypoint>& tpo_wp_index)
{
//printf("Processing Waypoints...\n");
// Fetch storage for the waypoint index (needed later for
// routes)
- tpo_wp_index = (Waypoint**) xmalloc(sizeof(Waypoint*) * waypoint_count);
- tpo_index_ptr = 0;
+ tpo_wp_index.clear();
+ tpo_wp_index.reserve(waypoint_count);
if (waypoint_count == 0) {
return;
// For routes (later), we need a duplicate of each waypoint
// indexed by the order we read them in.
- auto* waypoint_temp2 = new Waypoint(*waypoint_temp);
-
- // Attach the copy to our index
- tpo_wp_index[tpo_index_ptr++] = waypoint_temp2;
+ // Attach a copy to our index.
+ tpo_wp_index.append(*waypoint_temp);
// Add the original waypoint to the chain of waypoints
waypt_add(waypoint_temp);
// Route decoder for version 3.x files.
//
-// We depend on tpo_wp_index[] having been malloc'ed and filled-in
-// with pointers to waypoint objects by tpo_process_waypoints()
+// We depend on tpo_wp_index having been filled-in
+// with waypoint objects by the tpo_process_waypoints()
// function above.
//
-void TpoFormatBase::tpo_process_routes()
+void TpoFormatBase::tpo_process_routes(const QList<Waypoint>& tpo_wp_index)
{
//printf("Processing Routes...\n");
//printf("val: %x\t\t", val);
// Duplicate a waypoint from our index of waypoints.
- auto* waypoint_temp = new Waypoint(*tpo_wp_index[val-1]);
+ auto* waypoint_temp = new Waypoint(tpo_wp_index[val-1]);
// Add the waypoint to the route
route_add_wpt(route_temp, waypoint_temp);
//
void TpoFormatBase::tpo_read_3_x()
{
+ // Global index to waypoints, needed for routes, filled in by
+ // tpo_process_waypoints.
+ //
+ // For version 3.x files.
+ QList<Waypoint> tpo_wp_index;
if (doing_trks) {
//printf("Processing Tracks\n");
if (doing_wpts || doing_rtes) {
//printf("Processing Waypoints\n");
- tpo_process_waypoints();
+ tpo_process_waypoints(tpo_wp_index);
}
if (doing_rtes) {
// for routes.
//
//printf("Processing Routes\n");
- tpo_process_routes();
+ tpo_process_routes(tpo_wp_index);
}
if (doing_wpts) {
void
TpoFormatBase::tpo_rd_init(const QString& fname)
{
-
- // prepare for an attempt to deallocate memory that may or may not get allocated
- // depending on the options used.
- tpo_index_ptr = 0;
- tpo_wp_index = nullptr;
-
tpo_file_in = gbfopen_le(fname, "rb", MYNAME);
tpo_check_version_string();
void
TpoFormatBase::tpo_rd_deinit()
{
- // Free the waypoint index, we don't need it anymore.
- for (unsigned int i = 0; i < tpo_index_ptr; i++) {
- delete tpo_wp_index[i];
- }
- tpo_index_ptr = 0;
-
- // Free the index array itself
- if (tpo_wp_index) {
- xfree(tpo_wp_index);
- tpo_wp_index = nullptr;
- }
-
gbfclose(tpo_file_in);
}
*/
#include <algorithm> // for sort
-#include <cctype> // for isspace, isalpha, ispunct, tolower, toupper
+#include <cctype> // for isspace, tolower
#include <cerrno> // for errno
#include <climits> // for INT_MAX, INT_MIN
#include <cmath> // for fabs, floor
*/
QString
-convert_human_date_format(const char* human_datef)
+convert_human_date_format(const QString& human_datef)
{
- char* result = (char*) xcalloc((2*strlen(human_datef)) + 1, 1);
- char* cout = result;
- char prev = '\0';
+ QString result;
+ QChar prev = '\0';
int ylen = 0;
- for (const char* cin = human_datef; *cin; cin++) {
- char okay = 1;
+ for (const QChar cin : human_datef) {
+ bool okay = true;
- if (toupper(*cin) != 'Y') {
+ if (cin.toUpper() != 'Y') {
ylen = 0;
}
- if (isalpha(*cin)) {
- switch (*cin) {
+ if (cin.isLetter()) {
+ switch (cin.unicode()) {
case 'y':
case 'Y':
if (prev != 'Y') {
- strcat(cout, "%y");
- cout += 2;
+ result.append("%y");
prev = 'Y';
}
ylen++;
if (ylen > 2) {
- *(cout-1) = 'Y';
+ result.back() = 'Y';
}
break;
case 'm':
case 'M':
if (prev != 'M') {
- strcat(cout, "%m");
- cout += 2;
+ result.append("%m");
prev = 'M';
}
break;
case 'd':
case 'D':
if (prev != 'D') {
- strcat(cout, "%d");
- cout += 2;
+ result.append("%d");
prev = 'D';
}
break;
default:
- okay = 0;
+ okay = false;
}
- } else if (ispunct(*cin)) {
- *cout++ = *cin;
+ } else if (cin.isPunct()) {
+ result.append(cin);
prev = '\0';
} else {
- okay = 0;
+ okay = false;
}
- if (okay == 0) {
- fatal("Invalid character \"%c\" in date format \"%s\"!\n", *cin, human_datef);
+ if (!okay) {
+ fatal(FatalMsg().nospace() << "Invalid character " << cin << " in date format " << human_datef << "!");
}
}
- QString rv(result);
- xfree(result);
- return rv;
+ return result;
}
/*
*/
QString
-convert_human_time_format(const char* human_timef)
+convert_human_time_format(const QString& human_timef)
{
- char* result = (char*) xcalloc((2*strlen(human_timef)) + 1, 1);
- char* cout = result;
- char prev = '\0';
+ QString result;
+ QChar prev = '\0';
- for (const char* cin = human_timef; *cin; cin++) {
- int okay = 1;
+ for (const QChar cin : human_timef) {
+ bool okay = true;
- if (isalpha(*cin)) {
- switch (*cin) {
+ if (cin.isLetter()) {
+ switch (cin.unicode()) {
case 'S':
case 's':
if (prev != 'S') {
- strcat(cout, "%S");
- cout += 2;
+ result.append("%S");
prev = 'S';
}
break;
case 'M':
case 'm':
if (prev != 'M') {
- strcat(cout, "%M");
- cout += 2;
+ result.append("%M");
prev = 'M';
}
break;
case 'h': /* 12-hour-clock */
if (prev != 'H') {
- strcat(cout, "%l"); /* 1 .. 12 */
- cout += 2;
+ result.append("%l"); /* 1 .. 12 */
prev = 'H';
} else {
- *(cout-1) = 'I'; /* 01 .. 12 */
+ result.back() = 'I'; /* 01 .. 12 */
}
break;
case 'H': /* 24-hour-clock */
if (prev != 'H') {
- strcat(cout, "%k");
- cout += 2;
+ result.append("%k");
prev = 'H';
} else {
- *(cout-1) = 'H';
+ result.back() = 'H';
}
break;
case 'x':
if (prev != 'X') {
- strcat(cout, "%P");
- cout += 2;
+ result.append("%P");
prev = 'X';
} else {
- *(cout-1) = 'P';
+ result.back() = 'P';
}
break;
case 'X':
if (prev != 'X') {
- strcat(cout, "%p");
- cout += 2;
+ result.append("%p");
prev = 'X';
} else {
- *(cout-1) = 'p';
+ result.back() = 'p';
}
break;
default:
- okay = 0;
+ okay = false;
}
- } else if (ispunct(*cin) || isspace(*cin)) {
- *cout++ = *cin;
+ } else if (cin.isPunct() || cin.isSpace()) {
+ result.append(cin);
prev = '\0';
} else {
- okay = 0;
+ okay = false;
}
- if (okay == 0) {
- fatal("Invalid character \"%c\" in time format \"%s\"!\n", *cin, human_timef);
+ if (!okay) {
+ fatal(FatalMsg().nospace() << "Invalid character " << cin << " in time format " << human_timef << "!");
}
}
- QString rv(result);
- xfree(result);
- return rv;
+ return result;
}